package com.company.service;

import com.company.data.UserDate;
import com.company.entity.Account;

public class SendMessage implements Runnable {
    private Account user;

    public SendMessage(Account user) {
        this.user = user;
    }

    @Override
    public void run() {
        while (true) {
            try {
                String msg = user.getRead().readLine();
                for (Account u : UserDate.USERS) {
                    if (!this.user.getName().equals(u.getName())) {
                        u.getWrite().println("【" + this.user.getName() + "】:" + msg);
                    }
                }
            } catch (Exception e) {
                UserDate.USERS.remove(this.user);
                for (Account u : UserDate.USERS) {
                    u.getWrite().println("【" + this.user.getName() + "】退出了聊天室,当前在线人数" + UserDate.USERS.size());
                }
                break;
            }
        }
    }
}
